import { Result } from './../../../../model/Result.d';
import { Component, Injector } from '@angular/core';
import { IonicPage} from 'ionic-angular';
import { BasePage } from '../../../basePage';
import { StorageService } from './../../../../providers/StorageService';
import { UserService } from '../../../../providers/UserService';
import {  nailerService } from './../../../../providers/nailerService';
import { PIC_URL } from "./../../../../providers/Config";
/**
 * Generated class for the BillPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-bill',
  templateUrl: 'bill.html',
})
export class BillPage extends BasePage{

  billList=[];  //美甲师的账单
  billList2=[]; //店铺的收益
  billList3=[]; //消费者支出账单
  
  param2={
    user_id:0,
    all:1
  }
  page=1
  userinfo:{}   //读取的用户信息
  picrooturl; //图片服务地址
  
  who="";   //从点击账单传来的身份
  vip:boolean=false;   //消费者帐单显示器
  nail:boolean=false;  //美甲师账单显示器
  shop:boolean=false;   //美甲店主账单显示器
  bottom:boolean=false; //美甲师底部
  bottom2:boolean=false;  //美甲店底部
  bottom3:boolean = false;  //消费者底部
  notCos:boolean=true;           //提现记录

  isBuy:boolean;  //店铺是购买还是出售
  scanObj={};   //从四个图片点过来的参数，包括身份，是否查看当天，查看那种类型

  constructor(injector: Injector, public nailerService: nailerService,public stc: StorageService, public userService: UserService) {
    super(injector);
    this.picrooturl = PIC_URL;
    this.userinfo = this.stc.read('userinfo');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad BillPage');
    this.who=this.navParams.get("identity");  //从上个页面的点击了账单传来的身份
    this.scanObj=this.navParams.get("date");  //从上个页面四个按钮点击后传来的身份及其他参数
    console.log(this.who);
    console.log(this.scanObj);
    //如果是点账单进来的   先判断身份确定显示那个身份的账单
    if(this.scanObj == undefined){
      if(this.navParams.get("identity")=="消费者"){
        //显示并请求数据
        this.vip=true;
        this.notCos=false;
        this.getVip(''); //此处请求为第一次请求，不需要滚动事件
      }else if(this.navParams.get("identity")=="美甲师"){
        this.nail=true;
        this.artParam('');
      }else if(this.navParams.get("identity")=="美甲店主"){
        this.shop=true;
        this.shopParam('');
      }
      //如果是点四个按钮进来的
    }else{
      if(this.scanObj['identity']=="美甲师"){
        this.nail=true;
        this.notCos=false;
        this.artParam(''); //此处请求为第一次请求，不需要滚动事件
      }else if(this.scanObj['identity']=="美甲店主"){
        this.shop=true;
        this.notCos=false;
        this.shopParam('');
      }
    }
  }

  dismissCLI() {
    this.viewCtrl.dismiss();
  }

  /**获取消费者支出*/
  async getVip(infiniteScroll){
    let param={user_id:this.userinfo['user_id'],pop:this.page,size:10};
    let result=await this.userService.userPayListAPI(param);
    console.log(result);
    if(result.code==1){
      this.page++;
      //点击账单进来的
      //if(this.scanObj == undefined || this.scanObj['kind'] == 4){
      //if(this.scanObj == undefined){
        console.log("查看总账单")
        this.billList3= this.billList3.concat(result.data.buy_service_list);
        if(infiniteScroll){
          infiniteScroll.complete();
          if(result.data.buy_service_list.length<10){
            infiniteScroll.enable(false);
            this.bottom3 = true;
          }
        }
      //}
      // else if(this.scanObj['identity']=="消费者" && this.scanObj['kind'] == 2){    //点击了查看当天支出
      //   console.log("查看当天支出")
      //   this.billList3=this.billList3.concat(result.data.today_service_list);
      //   if(infiniteScroll){
      //     infiniteScroll.complete();
      //     if(result.data.today_service_list.length<10){
      //       infiniteScroll.enable(false);
      //       this.bottom3 = true;
      //     }
      //   }
      // }
    }else{
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
    }
  }

  /**
   * 确定美甲师传什么参数
   * @param infiniteScroll 
   */
  artParam(infiniteScroll){
    if(this.scanObj == undefined){  //点击账单查看的是总的
      let param={user_id:this.userinfo['user_id'],all:1,pop:this.page,size:10}
      this.getBill(param,infiniteScroll);
    }else if(this.scanObj['kind'] == 2){  //查看当天收益
      console.log("查看今天收益")
      let param={user_id:this.userinfo['user_id'],all:3,pop:this.page,size:10}
      this.getBill(param,infiniteScroll);
    }else if(this.scanObj['kind'] == 3){    //查看总收益
      console.log("查看总收益")
      let param={user_id:this.userinfo['user_id'],all:2,pop:this.page,size:10}
      this.getBill(param,infiniteScroll);
    }

  }

  /**
   * 获取美甲师数据*
   * @param p   //向后台传递的参数
   * @param infiniteScroll  滚动事件
   */
  async getBill(p,infiniteScroll){
    //获取美甲师的账单
    let result = await this.nailerService.artistIncomeAPI(p);  
    console.log(result)
    console.log(p)
    if(result.code==0){
      this.page++;
      this.billList = this.billList.concat(result.data.income_list);
        if(infiniteScroll){
          infiniteScroll.complete();
          if(result.data.income_list.length<10){
            infiniteScroll.enable(false);
            this.bottom=true;
          }
        }
        // }else if(this.scanObj['kind'] == 3){    //查看总收益
        //   let listWithot=result.data.income_list.filter((e)=>{
        //     return !e.pass_flag;
        //   })
        //   console.log(listWithot);
        //   this.billList=this.billList.concat(listWithot);
        //   if(infiniteScroll){
        //     infiniteScroll.complete();
        //     if(result.data.income_list.length<10){
        //       infiniteScroll.enable(false);
        //       this.bottom=true;
        //     }
        //   }
    }else{
      let toast = this.toastCtrl.create({
        message: result.msg,
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
    }
  }

  /**
   * 美甲店传到后台的参数
   */
  shopParam(infiniteScroll){
    //因为美店跟前两个不一样，列表和总的是分开的，所以要分开传参数
    if(this.scanObj==undefined){    //如果是点击账单进入或点击四个按钮里总的，则显示总
      console.log("查看月份")
      let param2={user_id:this.userinfo['user_id'],all:1,pop:this.page,size:10}
      this.getBill2(param2,infiniteScroll);
    }else if(this.scanObj['kind'] == 1){        //如果是点击四个按钮当天收益， 则显示当天
      console.log("查看当天收益")
      let param2={user_id:this.userinfo['user_id'],all:2,pop:this.page,size:10}
      this.getBill2(param2,infiniteScroll);
    }else if(this.scanObj['kind'] == 2){        //如果是点击四个按钮当天支出，则显示当天支出
      console.log("查看当天支出")
      let param2={user_id:this.userinfo['user_id'],all:3,pop:this.page,size:10}
      this.getBill2(param2,infiniteScroll);
    }else if(this.scanObj['kind'] == 3){       //如果是点击四个按总收益
      console.log("查看总收益")
      let param2={user_id:this.userinfo['user_id'],all:4,pop:this.page,size:10}
      this.getBill2(param2,infiniteScroll);
    }else if(this.scanObj['kind'] == 4){       //如果是点击四个按钮总支出
      console.log("查看总支出");
      let param2={user_id:this.userinfo['user_id'],all:5,pop:this.page,size:10}
      this.getBill2(param2,infiniteScroll);
    }
   
  }
  /**
   * 获取美甲店账单
   * @param p 具体向后台传的参数
   * @param infiniteScroll 滚动事件
   */
  async getBill2(p,infiniteScroll){
    console.log(p,infiniteScroll);
    let result2= await this.nailerService.showShopIncomeListAPI(p);
    console.log(result2.data);
    if(result2.code== 1){
      this.page++;
      //点击账单进入总的
      // if(this.scanObj == undefined){
        console.log("账单"+this.page)
        this.billList2=this.billList2.concat(result2.data);
        if(infiniteScroll){
          infiniteScroll.complete(); //请求完成数据更新页面
          if(result2.data.length<10){
            infiniteScroll.enable(false);
            this.bottom2=true;
          }
        }
      // }else if(this.scanObj['isToday'] ==0 && this.scanObj['isIn']== 0){    //点击四个按钮中的当天收益
      //   console.log("当天收益"+this.page)
      //   this.billList2=this.billList2.concat(result2.data)
      //   if(infiniteScroll){
      //     infiniteScroll.complete(); //请求完成数据更新页面
      //     if(result2.data.length<6){
      //       infiniteScroll.enable(false);
      //     }
      //   }
      // }else if(this.scanObj['isToday'] ==0 && this.scanObj['isIn']== 1){    //点击四个按钮中的当天支出
      //   console.log("当天支出"+this.page)
      //   this.billList2=this.billList2.concat(result2.data)
      //   if(infiniteScroll){
      //     infiniteScroll.complete(); //请求完成数据更新页面
      //     if(result2.data.length<6){
      //       infiniteScroll.enable(false);
      //     }
      //   }
      // }else if(this.scanObj['isToday'] ==1 && this.scanObj['isIn'] == 0){
      //   console.log("当天支出"+this.page)
      //   this.billList2=this.billList2.concat(result2.data)
      //   if(infiniteScroll){
      //     infiniteScroll.complete(); //请求完成数据更新页面
      //     if(result2.data.length<6){
      //       infiniteScroll.enable(false);
      //     }
      //   }
      // }
      
    }else{
      let toast = this.toastCtrl.create({
        message: result2.msg,
        duration: 1500,
        position: 'bottom',
      })
      toast.present();
    }  
  }
  /*跳转详情*/
  gotoBillDetail(i){
    console.log(i);
    this.navCtrl.push("BillDetailPage",{'order_sn':i.order_sn});
  }
  gotoBillDetail2(v){
    console.log(v);
    let catogary
    if(!v['pass_flag'] && v['order_price'].indexOf("-") != -1){
      catogary="购买款式"
    }else if(!v['pass_flag'] && v['order_price'].indexOf("+") != -1){
      catogary="售出服务"
    }
    this.navCtrl.push("BillDetailPage",{'order_sn2':v.order_sn,'cato':catogary});
  }
  gotoBillDetail3(i){
    this.navCtrl.push("BillDetailPage",{'order_sn3':i.order_sn});
  }

  //分页
  doInfinite1(infiniteScroll){
    this.getVip(infiniteScroll)
  }
  doInfinite2(infiniteScroll){
    this.artParam(infiniteScroll);
  }
  doInfinite3(infiniteScroll){
    this.shopParam(infiniteScroll);
  }
  //提现记录
  gotoRecorde(){
    this.navCtrl.push('RecordePage',{types:this.who})
  }
}
